package com.ycsj.common.manager;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.ycsj.common.R;
import com.ycsj.common.view.pop.CommonPopupWindow;


/**
 * Created by Administrator on 2018/1/30 0030.
 */

public class PopUpManager {

    public static CommonPopupWindow popupWindow;

    /**
     * 显示PopupWindow的方法
     */
    public static void showPop(final Context context, int layoutId, float level, View view, final onGetViewListener listener) {
        //防止对象过多创建
        if (popupWindow != null) {
            popupWindow = null;
        }
        popupWindow = new CommonPopupWindow.Builder(context)
                .setView(layoutId)//设置视图
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)//设置宽高
                .setAnimationStyle(R.style.common_popwindow_anim_style)//设置动画
                .setBackGroundLevel(level)
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        if (listener != null) {
                            listener.getChildView(view, layoutResId);
                        }
                    }
                })//设置PopupWindow里的子View及点击事件
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();

        //弹出PopupWindow
        popupWindow.showAsDropDown(view,0,-4);
    }


    /**
     * 屏幕中间显示PopUpWinodw的方法WRAP_CONTENT
     */
    public static void showPop(final Context context, int layoutId, float level, Activity activity, final onGetViewListener listener) {
        //防止对象过多创建
        if (popupWindow != null) {
            popupWindow = null;
        }
        popupWindow = new CommonPopupWindow.Builder(context)
                .setView(layoutId)//设置视图
                .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)//设置宽高
                .setAnimationStyle(R.style.common_pop_anim_style)//设置动画
                .setBackGroundLevel(level)
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        if (listener != null) {
                            listener.getChildView(view, layoutResId);
                        }
                    }
                })//设置PopupWindow里的子View及点击事件
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();

        //弹出PopupWindow
        popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }


    /**
     * 屏幕中间显示PopUpWinodw的方法宽度MATCH_PARENT
     */
    public static void showPopByMatchParent(final Context context, int layoutId, float level, Activity activity, final onGetViewListener listener) {
        //防止对象过多创建
        if (popupWindow != null) {
            popupWindow = null;
        }
        popupWindow = new CommonPopupWindow.Builder(context)
                .setView(layoutId)//设置视图
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)//设置宽高
                .setAnimationStyle(R.style.common_pop_anim_style)//设置动画
                .setBackGroundLevel(level)
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        if (listener != null) {
                            listener.getChildView(view, layoutResId);
                        }
                    }
                })//设置PopupWindow里的子View及点击事件
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();

        //弹出PopupWindow
        popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }


    /**
     * 屏幕中间显示PopUpWinodw的方法,带消失接口的监听
     */
    public static void showPop(final Context context, int layoutId, float level, Activity activity, final onGetViewListener listener, final onDismissListener onDismissListener) {
        //防止对象过多创建
        if (popupWindow != null) {
            popupWindow = null;
        }
        popupWindow = new CommonPopupWindow.Builder(context)
                .setView(layoutId)//设置视图
                .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)//设置宽高
                .setAnimationStyle(R.style.common_pop_anim_style)//设置动画
                .setBackGroundLevel(level)
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        if (listener != null) {
                            listener.getChildView(view, layoutResId);
                        }
                    }
                })//设置PopupWindow里的子View及点击事件
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (onDismissListener != null) {
                    onDismissListener.onDismiss();
                }
            }
        });

        //弹出PopupWindow
        popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }


    /**
     * 屏幕中间显示PopUpWinodw并且点击空白和返回键都不消失的方法
     */
    public static void showPopWithNoCancel(final Context context, int layoutId, float level, Activity activity, final onGetViewListener listener) {
        //防止对象过多创建
        if (popupWindow != null) {
            popupWindow = null;
        }
        popupWindow = new CommonPopupWindow.Builder(context)
                .setView(layoutId)//设置视图
                .setOutsideTouchable(false)//点击外部不可消失
                .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)//设置宽高
                .setAnimationStyle(R.style.common_pop_anim_style)//设置动画
                .setBackGroundLevel(level)
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        if (listener != null) {
                            listener.getChildView(view, layoutResId);
                        }
                    }
                })//设置PopupWindow里的子View及点击事件
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();
        //调用
//        setNoCancelSate(popupWindow, popupWindow.getWidth(), popupWindow.getHeight());

        //弹出PopupWindow
        popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }


    /**
     * 屏幕中间显示PopUpWinodw的方法
     */
    public static void showPopWithMatch(final Context context, int layoutId, float level, Activity activity, final onGetViewListener listener) {
        //防止对象过多创建
        if (popupWindow != null) {
            popupWindow = null;
        }
        popupWindow = new CommonPopupWindow.Builder(context)
                .setView(layoutId)//设置视图
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT)//设置宽高
                .setAnimationStyle(R.style.common_pop_anim_style)//设置动画
                .setBackGroundLevel(level)
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        if (listener != null) {
                            listener.getChildView(view, layoutResId);
                        }
                    }
                })//设置PopupWindow里的子View及点击事件
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(false)
                //开始构建
                .create();

        //弹出PopupWindow
        popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }


    /**
     * 屏幕底部弹出PopUpWinodw的方法
     */
    public static void showPop(final Activity activity, int layoutId, float level, final onGetViewListener listener) {
        //防止对象过多创建
        if (popupWindow != null) {
            popupWindow = null;
        }
        popupWindow = new CommonPopupWindow.Builder(activity)
                .setView(layoutId)//设置视图
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)//设置宽高
                .setAnimationStyle(R.style.common_bottompopwindow_anim_style)//设置动画
                .setBackGroundLevel(level)
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        if (listener != null) {
                            listener.getChildView(view, layoutResId);
                        }
                    }
                })//设置PopupWindow里的子View及点击事件
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();

        //弹出PopupWindow
        popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }


    /**
     * 显示PopupWindow的方法
     */
    public static void showPopsort(final Context context, int layoutId, float level, View view, final onGetViewListener listener) {
        //防止对象过多创建
        if (popupWindow != null) {
            popupWindow = null;
        }
        popupWindow = new CommonPopupWindow.Builder(context)
                .setView(layoutId)//设置视图
                .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)//设置宽高
                .setAnimationStyle(R.style.common_popwindow_anim_style)//设置动画
                .setBackGroundLevel(level)
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        if (listener != null) {
                            listener.getChildView(view, layoutResId);
                        }
                    }
                })//设置PopupWindow里的子View及点击事件
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();

        //弹出PopupWindow
        popupWindow.showAsDropDown(view);
    }

    /**
     * 关闭PopupWindow的方法
     */
    public static void dismiss() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    /**
     * Android6.0之前和之后设置PopupWindow点开空白和返回键消失和禁止消失的设置
     *
     * @param pWindow
     * @param mWidth
     * @param mHeight
     */
    public static void setNoCancelSate(PopupWindow pWindow, final int mWidth, final int mHeight) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //大于6.0
            //在Android 6.0以上 ，只能通过拦截事件来解决
            pWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    final int x = (int) event.getX();
                    final int y = (int) event.getY();

                    if ((event.getAction() == MotionEvent.ACTION_DOWN)
                            && ((x < 0) || (x >= mWidth) || (y < 0) || (y >= mHeight))) {
                        // donothing
                        // 消费事件
                        return false;
                    } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        Log.e("test", "out side ...");
                        return true;
                    }
                    return false;
                }
            });
        } else {
            //小于6.0
            //6.0之前，返回键的控制
            pWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        // popupWindow.dismiss();

                        return false;
                    }
                    return false;
                }
            });
        }
    }


    /**
     * 回调接口,用于获取弹框视图控件便于做设置数据的处理
     */
    public interface onGetViewListener {
        void getChildView(View view, int layoutResId);
    }

    /**
     * 用于PopUpWindow消失的接口
     */
    public interface onDismissListener {
        void onDismiss();
    }
}
